using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

using Impact.Infrastructure.Plugins;
using System.Web.UI.HtmlControls;
using System.Web;
using Impact.Infrastructure;
using System.Web.UI;

namespace Impact.Plugins
{
    /// <summary>
    /// Plugin for finding nearest profiles based on zipcode
    /// </summary>
    public class FindNearestProfilePlugin : Impact.Infrastructure.Plugins.IImpactPlugin
    {

        private static Guid id = new Guid( "E903C9BE-1FFF-4314-909B-2E0622A562FF" );
        private static Dictionary<string, PluginParameter> parameters;

        #region IImpactPlugin Members

        /// <summary>
        /// unique id of this plugin
        /// </summary>
        public Guid PluginId
        {
            get { return id; }
        }

        /// <summary>
        /// textual name of this plugin
        /// </summary>
        public string Name
        {
            get { return "Find Nearest Profile"; }
        }

        /// <summary>
        /// parameters used by this plugin
        /// </summary>
        public Dictionary<string, PluginParameter> Parameters
        {
            get
            {
                if ( parameters == null )
                {
                    parameters = new Dictionary<string, PluginParameter>();
                    parameters.Add( "ResultsPage", new PluginParameter( this, "Results Page", typeof( string ), true ) );
                    parameters.Add( "SearchButtonImage", new PluginParameter( this, "Search Button Image", typeof( string ), false ) );
                }
                return parameters;
            }
        }

        /// <summary>
        /// admin interface for this plugin
        /// </summary>
        public IPluginAdmin Admin
        {
            get { return null; }
        }

        /// <summary>
        /// Creates the controls for displaying the plugin and adds them to the container parameter
        /// </summary>
        public void Render( Control container, Dictionary<string, PluginParameter> parameterValues )
        {

            HtmlGenericControl div = new HtmlGenericControl( "div" );
            div.Attributes.Add( "id", "findNearest" );
            div.Attributes.Add( "class", "minibox" );
            container.Controls.Add( div );

            Label title = new Label();
            title.Text = "Zip Code";
            div.Controls.Add( title );

            div.Controls.Add( new HtmlGenericControl( "br" ) );

            TextBox zipcode = new TextBox();
            zipcode.ID = "zipcode";
            div.Controls.Add( zipcode );

            WebControl submitButton;
            if ( !string.IsNullOrEmpty( parameterValues["SearchButtonImage"].Value ) )
            {
                submitButton = new ImageButton();
                ( (ImageButton)submitButton ).ImageUrl = parameterValues["SearchButtonImage"].Value;
                ( (ImageButton)submitButton ).Command += new CommandEventHandler( SubmitButton_Command );
            }
            else
            {
                submitButton = new Button();
                ( (Button)submitButton ).Text = "Search";
                ( (Button)submitButton ).Command += new CommandEventHandler( SubmitButton_Command );
            }
            submitButton.ID = "submit";
            div.Controls.Add( submitButton );

            this.container = container;
            this.parameterValues = parameterValues;
        }

        private void SubmitButton_Command( object sender, CommandEventArgs e )
        {
            TextBox zipcode = (TextBox)Utility.FindControlRecursive( container.Controls, "zipcode" );

            if ( !string.IsNullOrEmpty( zipcode.Text ) )
            {
                HttpContext.Current.Response.Redirect( string.Format( "{0}?zip={1}", parameterValues["ResultsPage"].Value, zipcode.Text ) );
            }
        }

        private Control container;
        private Dictionary<string, PluginParameter> parameterValues;

        #endregion
    }
}
